package com.tvshows.front.view.favorite;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.tvshows.front.controllers.favorite.FavoriteController;
import com.tvshows.front.listener.list.ListListener;
import com.tvshows.front.ui.favorite.ListCompositeFavoriteUI;
import com.tvshows.front.view.labels.FontAssociation;
import com.tvshows.structure.beans.Favorite;

/**
 * @author tMidy
 * <p>
 * Implementation of {@link ListCompositeFavoriteUI}
 * </p>
 */
public class ListCompositeFavoriteView
    extends Composite
    implements ListCompositeFavoriteUI
{

    /**
     * form data measure
     */
    private static final Integer DATA_230 = 230;

    /**
     * The favorite table
     */
    private Table favoriteTable;

    /**
     * The controller of the view
     */
    private FavoriteController controller;

    /**
     * Constructeur
     * 
     * @param parent composite parent
     * @param style le style
     */
    public ListCompositeFavoriteView( Composite parent, int style )
    {
        super( parent, style );
        this.setLayout( new FillLayout() );
        initTable();
    }

    /**
     * Inits the table
     */
    private void initTable()
    {
        favoriteTable = new Table( this, SWT.SINGLE | SWT.SCROLL_LINE );
        /*
         * The show name column
         */
        TableColumn nameFavoriteColumn = new TableColumn( favoriteTable, SWT.NONE );
        nameFavoriteColumn.setText( "Nom de la série" );
        nameFavoriteColumn.setWidth( DATA_230 );
        nameFavoriteColumn.setResizable( false );
        favoriteTable.setHeaderVisible( true );

        /*
         * The listener of the list
         */
        ListListener listener = new ListListener( this );
        favoriteTable.addMouseListener( listener );
    }

    /**
     * Refresh the favorites table
     */
    public void loadFavorites( List<Favorite> favorites )
    {
        favoriteTable.removeAll();
        for ( Favorite favorite : favorites )
        {
            TableItem itemFavorite = new TableItem( favoriteTable, 0 );
            itemFavorite.setFont( FontAssociation.getFont( getDisplay() ) );
            itemFavorite.setText( favorite.getNameFavorite() );
        }
        favoriteTable.update();
        favoriteTable.layout();
        this.getShell().update();
    }

    /**
     * @return the favoriteTable
     */
    public Table getFavoriteTable()
    {
        return favoriteTable;
    }

    /**
     * @see com.tvshows.front.ui.favorite.ListCompositeFavoriteUI#majDelete()
     */
    @Override
    public void majDelete()
    {
        controller.enableDelete( getFavoriteTable().getSelectionCount() == 1 );
    }

    /**
     * @see com.tvshows.front.ui.favorite.ListCompositeFavoriteUI#getSelectedFavorite()
     */
    @Override
    public String getSelectedFavorite()
    {
        if ( getFavoriteTable().getSelectionCount() != 0 )
        {
            return getFavoriteTable().getSelection()[0].getText();
        }
        return "NoSelection";
    }

    /**
     * @see com.tvshows.front.ui.favorite.ListCompositeFavoriteUI#setController(com.tvshows.front.controllers.favorite.FavoriteController)
     */
    @Override
    public void setController( FavoriteController favoriteController )
    {
        this.controller = favoriteController;
    }

    /**
     * @see com.tvshows.front.ui.favorite.ListCompositeFavoriteUI#isToModify()
     */
    @Override
    public boolean isToModify()
    {
        return getFavoriteTable().getSelectionCount() > 0;
    }

    /************ UNUSED *****************/

    @Override
    public void initFormData()
    {

    }

    @Override
    public void initComposites()
    {

    }
}
